<template>
  <div class="app-container">
    <el-form :inline="true">
      <el-form-item label="标题:">
        <el-input v-model="queryParams.title" placeholder="请输入轮播图标题" />
      </el-form-item>
      <el-form-item label="描述:">
        <el-input v-model="queryParams.description" placeholder="请输入轮播图标题" />
      </el-form-item>
      <el-form-item label="创建时间:">
        <el-date-picker
          v-model="dateRange"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery('1')">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="handleQuery('reset')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb5">
      <el-col :span="1.5"><el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button></el-col>
      <el-col :span="1.5"><el-button type="success" :disabled="single" icon="el-icon-edit" @click="handleUpdate(null)">修改</el-button></el-col>
      <el-col :span="1.5"><el-button type="danger" :disabled="multiple" icon="el-icon-delete" @click="handleDelete(null)">删除</el-button></el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" />
      <el-table-column prop="title" label="轮播图标题" min-width="80px" />
      <el-table-column prop="url" label="链接" />
      <el-table-column prop="description" label="描述内容" min-width="150px" />
      <el-table-column prop="clickCount" label="点击次数" min-width="60px" />
      <el-table-column prop="status" label="是否显示" min-width="60px">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-value="1" inactive-value="0" @change="handleStatusChange(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="图片" prop="imgUrl" min-width="70px">
        <template slot-scope="scope">
          <el-image
            style="width: 30px; height: 30px"
            :src="scope.row.imgUrl"
            :preview-src-list="[scope.row.imgUrl]"
          />
        </template>
      </el-table-column>
      <el-table-column prop="target" label="打开新窗口" min-width="70px">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.target" active-value="1" inactive-value="0" @change="handleTargetChange(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建日期" min-width="120px" />
      <el-table-column label="操作" min-width="120px">
        <template slot-scope="scope">
          <el-button type="text" size="mini" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="text" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialogVisible" :title="title" width="450px">
      <el-form ref="edit" :model="form" :label-width="formLabelWidth">
        <el-form-item label="轮播图标题">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="链接地址">
          <el-input v-model="form.url" />
        </el-form-item>
        <el-form-item label="图片地址">
          <el-input v-model="form.imgUrl" />
        </el-form-item>
        <el-form-item label="显示状态">
          <el-radio-group v-model="form.status">
            <el-radio v-for="item in statusOptions" :key="item.dictValue" :label="item.dictValue">{{ item.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="打开新窗口">
          <el-radio-group v-model="form.target">
            <el-radio v-for="item in openNewTargetOptions" :key="item.dictValue" :label="item.dictValue">{{ item.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描述内容">
          <el-input v-model="form.description" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import initData from '@/mixins/initData'
import { changeCarouselStatus, changeCarouselTarget, addCarousel, updateCarousel } from '@/api/system/carousel'

export default {
  mixins: [initData],
  data() {
    return {
      statusOptions: [],
      openNewTargetOptions: [],
      queryParams: {
        title: undefined,
        description: undefined,
        status: undefined
      },
      formLabelWidth: '100px',
      formReset: {
        status: '1'
      }
    }
  },
  mounted() {
    this.handleQuery()
    this.getDict('status_normal').then(res => {
      this.statusOptions = res.data
    })
    this.getDict('open_new_target').then(res => {
      this.openNewTargetOptions = res.data
    })
  },
  methods: {
    beforeInit() {
      this.base = '/system/carousel'
      this.modalName = '轮播图'
    },
    handleStatusChange(row) {
      const text = row.status === '0' ? '停用' : '启用'
      this.$confirm('确认要' + text + '【' + row.title + '】用户吗？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return changeCarouselStatus(row.id, row.status)
      }).then(res => {
        console.log(res)
        this.msgSuccess(text + '成功')
      }).catch(() => {
        row.status = row.status === '0' ? '1' : '0'
      })
    },
    handleTargetChange(row) {
      const text = row.target === '0' ? '停用' : '启用'
      this.$confirm('确认要' + text + '【' + row.title + '】轮播图吗？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return changeCarouselTarget(row.id, row.target)
      }).then(res => {
        console.log(res)
        this.msgSuccess(text + '成功')
      }).catch(() => {
        row.target = row.target === '0' ? '1' : '0'
      })
    },
    submitForm() {
      this.$refs['edit'].validate(valid => {
        if (valid) {
          if (!this.form.id) {
            addCarousel(this.form)
          } else {
            updateCarousel(this.form)
          }
          this.dialogVisible = false
          this.handleQuery()
        }
      })
    }
  }
}
</script>

<style>

</style>
